package com.itsci.assignment2;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class VehicleManager extends SQLiteOpenHelper{
	public VehicleManager(Context context, String name, CursorFactory factory,
			int version) {
		super(context, "Vehicle", null, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE Vehicle(vehicleId INTEGER  PRIMARY KEY AUTOINCREMENT ,NAME TEXT,price INTEGER);");
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE Vehicle");
		onCreate(db);
		
	}
	
	public void insertVehicle(VehicleBean v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put("name", v.getName());
		content.put("price", v.getPrice());
		db.insert("Vehicle", null, content);
		db.close();
	}
	
	public void updateVehicle(VehicleBean v) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put("name", v.getName());
		content.put("price", v.getPrice());
		db.update("Vehicle", content, "vehicleId = ?", new String[] { v.getId()+"" });
		db.close();
	}
	
	public void deleteVehicle(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Vehicle", "vehicleId = ?", new String[] { id+"" });
		db.close();
	}
	
	public int getId(String name){
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Vehicle", new String[] { "vehicleId","name","price"},
				"name = ?", new String[] { name }, null, null, null);
		int id = 0;
		if (cursor.moveToFirst()) {
		 id = Integer.parseInt(cursor.getString(0));
		}
		
		return id;
	}
	
	public VehicleBean getByVehicleId(String name) {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Vehicle", new String[] { "vehicleId","name","price"},
				"name = ?", new String[] { name }, null, null, null);
		VehicleBean v = new VehicleBean();
		if (cursor.moveToFirst()) {
			v.setName(cursor.getString(1));
			v.setPrice(Integer.parseInt(cursor.getString(2)));
		} else {
			v.setName("");
			v.setPrice(0);
	
		}
		db.close();
		return v;
	}
	
	public ArrayList<VehicleBean> getAllVehicleBean() {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Vehicle", new String[] {"vehicleId","name","price"}, null, null, null, null, null);
		ArrayList<VehicleBean> arrayVehicle = new ArrayList<VehicleBean>();
		if (cursor.moveToFirst()) {
			do {
				VehicleBean v = new VehicleBean();				
				v.setName(cursor.getString(1));
				v.setPrice(Integer.parseInt(cursor.getString(2)));
				arrayVehicle.add(v);
			}while(cursor.moveToNext());
		}
		db.close();
		return arrayVehicle;
	}
}
